<script>
	import Vue, { watch } from 'vue'
  import md5 from './static/js/md5.js';
  import vconsole  from 'vconsole'
	export default {
    data() {
      return {
        clickCount: 0,
        timer: null,
      }
    },
		onLaunch: function(e) {
      const that = this;
			const extConfig = uni.getExtConfigSync ? uni.getExtConfigSync() : {};
			if(extConfig.appid){
				this.$config.appId = extConfig.appid;
			}
      if (e.query.f) {
        uni.setStorageSync('userFrom', e.query.f)
      }
      
      that.initGuestInfo();
      
      that.initConsoleTool();
      
      // new vconsole();
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
    methods: {
      initGuestInfo () {
        const lastReportDate = uni.getStorageSync('lastReportDate');
        const _userInfo = uni.getStorageSync('userinfo');
        const _guestId =  uni.getStorageSync('guestId');
        let hasReportToday = true;
        if (!lastReportDate) {
          hasReportToday = false;
        } else {
          if (_userInfo || _guestId) {
            const reportDate = new Date(lastReportDate);
            const now = new Date();
            hasReportToday =  reportDate.getFullYear() === now.getFullYear() && reportDate.getMonth() === now.getMonth() && reportDate.getDate() === now.getDate();
            
            if (hasReportToday) {return false;}
          } else {
            hasReportToday = false;
          }
        }
        
        let pagePath = location.pathname + location.search;
        if (pagePath.indexOf('/preview?vid=') > -1) {
          pagePath = pagePath.replace('preview?vid=', 'preview/')
        }
        const requestData = {
          url: location.href,
          page: pagePath
        }
        if (_userInfo?.id) {
          requestData.user_id = _userInfo.id
        } else {
          // 获取guestid
          requestData.guest_id = _guestId ? _guestId : this.genrateNewGuestId();
        }
        requestData.plan_code = uni.getStorageSync('userFrom') || ''
        uni.request({
          url: this.configs.apiUrl + '/api/index/planUv',
          data: requestData,
          success() {
            const now = new Date();
            const today = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;
            uni.setStorageSync('lastReportDate', today);
          }
        })
        
      },
      genrateNewGuestId () {
        // 获取浏览器指纹信息
        const browserInfo = {
          userAgent: navigator.userAgent,
          language: navigator.language,
          platform: navigator.platform,
          screenSize: `${screen.width}x${screen.height}`,
        }
        // 转换为字符串  
        const browserFingerprint = JSON.stringify(browserInfo);
        
        const guestId = md5(browserFingerprint);
        uni.setStorageSync('guestId', guestId);
        return guestId;
      },
      initConsoleTool () {
        let count = 0;
        let timer = null;
        let isShow = false;
        document.body.addEventListener('click', function () {
          if(isShow){
            return;
          }
          if (timer != null) {
            clearTimeout(timer)
          }
          count++;
          // console.log('click', count)
          if (count == 10) {
            new vconsole();
            isShow = true
          }
          timer = setTimeout(()=>{
            count = 0;
          }, 500)
        })
      }
    },
    watch: {
      $route(to, from) {
        if (to.path != from.path) {
          this.GAHandle('page_view', '', {
            page_title: to.name,
            page_location: location.href,
          })
        }
      }
    }
	}
</script>

<style>
	@import url("./static/css/common.css");
</style>
